<?php $this->pageTitle = $invoice->invoice_type_code . ' Details'; ?>
<?php
$this->breadcrumbs = array(
	Yii::t('app', $invoice->invoice_type_code . '|' . $invoice->invoice_type_code . 's', 2) => array('/invoice/index', 'invoice_type' => $invoice->invoice_type_code),
	'#' . $invoice->invoice_number
);
?>
<div class="main-title no-border clearfix">
	<h1 class="float-left"><?php echo $invoice->invoice_type_code; ?> #<?php echo $invoice->invoice_number; ?></h1>
	<?php if ($invoice->deleted == 0): ?>
		<div class="controls">
			<a class="link-button with-icon" href="<?php echo $this->createUrl('/invoice/update', array('id' => $invoice->id)); ?>">
				<img src="/images/icons/fugue/pencil.png" />
				Edit
			</a>

			<a id="btnPrint" class="link-button with-icon split-button" href="<?php echo $this->createUrl('/invoice/print', array('id' => $invoice->id)); ?>" target="_blank">
				<img src="/images/icons/fugue/printer.png" />
				Print
			</a>
			<?php
			$this->widget('PsActionMenu', array(
				'buttonText' => '<span class="caret"></span>',
				'showIndicator' => false,
				'htmlOptions' => array(
					'class' => 'link-button split-button-select'
				),
				'actions' => array(
					'printInvoice' => array(
						'label' => 'Print ' . $invoice->invoice_type_code,
						'icon' => '/images/icons/fugue/document-invoice.png',
						'url' => array('/invoice/print', 'id' => $invoice->id),
						'target' => '_blank'
					),
					'printReminder' => array(
						'label' => 'Print Reminder',
						'icon' => '/images/icons/fugue/alarm-clock.png',
						'url' => array('/invoice/printReminder', 'id' => $invoice->id),
						'target' => '_blank'
					),
				)
			));
			?>
			<a id="btnEmail" class="link-button with-icon" href="<?php echo $this->createUrl('/invoice/email', array('id' => $invoice->id)); ?>">
				<img src="/images/icons/fugue/mail.png" width="16" height="16" />
				Email
			</a>
			<?php if ($invoice->isProformaInvoice()): ?>
				<?php if ($invoice->hasParent()): ?>
					<a id="btnView" class="link-button green with-icon" href="<?php echo $this->createUrl('/invoice/view', array('id' => $invoice->parent->id)); ?>">
						<img src="/images/icons/fugue/magnifier.png" width="16" height="16" />
						View Invoice
					</a>
				<?php else: ?>
					<a id="btnConvert" class="link-button green with-icon" href="<?php echo $this->createUrl('/invoice/createFromProforma', array('id' => $invoice->id)); ?>">
						<img src="/images/icons/fugue/mail.png" width="16" height="16" />
						Create Invoice
					</a>
				<?php endif; ?>
			<?php endif; ?>
			<?php
//			$this->widget('PsActionMenu', array(
//				'buttonText' => '<span class="caret"></span>',
//				'showIndicator' => false,
//				'htmlOptions' => array(
//					'class' => 'link-button split-button-select'
//				),
//				'actions' => array(
//					'emailInvoice' => array(
//						'label' => 'Email Invoice',
//						'icon' => '/images/icons/fugue/document-invoice.png',
//						'url' => array('/invoice/email', 'id' => $invoice->id),
//						'onClick' => 'js:prestige.invoice.view.onEmailInvoiceActionClick'
//					),
//					'emailReminder' => array(
//						'label' => 'Email Reminder',
//						'icon' => '/images/icons/fugue/alarm-clock.png',
//						'url' => array('/invoice/emailReminder', 'id' => $invoice->id),
//					),
//				)
//			));
			?>

			<?php if ($invoice->invoice_type_code == 'Invoice'): ?>
				<a class="link-button with-icon <?php echo ((($invoice->deleted == 0 && !$invoice->isPaid()) || $invoice->getBalance() > 0 ) ? "" : "disabled"); ?> " 
				   href="<?php echo (($invoice->deleted == 0 && !$invoice->isPaid()) ? $this->createUrl('/receipt/create', array('invoice_id' => $invoice->id, 'party_id' => $invoice->to_party_id)) : "javascript:void(0);"); ?>">
					<img src="/images/icons/fugue/money--plus.png" />
					Receive Payment
				</a>
			<?php endif; ?>
		</div>
	<?php else: ?>
		<h1 class="float-left red pad">(Canceled)</h1>
	<?php endif; ?>
	<?php if ($invoice->deleted == 0): ?>
		<div class="controls float-right">
			<a class="link-button with-icon" href="<?php echo $this->createUrl('/invoice/cancel', array('id' => $invoice->id)); ?>">
				<img src="/images/icons/fugue/cross-circle.png" />
				Cancel <?php echo $invoice->invoice_type_code; ?>
			</a>
		</div>
	<?php endif; ?>
</div>
<div class="flat_area">
	<div class="grid_11 relative">
		<div class="section">
			<div class="block">
				<h2>Invoice Details</h2>
				<?php
				$this->widget('zii.widgets.CDetailView', array(
					'data' => $invoice,
					'attributes' => array(
						array(
							'label' => 'Bill To',
							'type' => 'raw',
							'value' => nl2br($invoice->bill_to_name)
						),
						array(
							'label' => 'C/O To',
							'type' => 'raw',
							'value' => $invoice->care_of
						),
						array(
							'label' => 'Attn To',
							'type' => 'raw',
							'value' => $invoice->attn
						),
						array(
							'label' => 'Address',
							'type' => 'raw',
							'value' => nl2br($invoice->bill_to_address)
						),
						'terms',
						array(
							'label' => 'Miscellaneous Info',
							'value' => $invoice->description
						),
						array(
							'label' => 'Salesperson\'s Copy',
							'type' => 'raw',
							'value' => $invoice->visible_to_sp ? 'Shown' : 'Not Shown'
						),
					),
				));
				?>
			</div>
		</div>
	</div>
	<div class="grid_5">
		<div class="section highlighted">
			<div class="block">
				<?php
				$this->widget('zii.widgets.CDetailView', array(
					'data' => $invoice,
					'attributes' => array(
						array(
							'label' => 'Status',
							'type' => 'raw',
							'value' => $invoice->getStatusFormatted()
						),
						array(
							'label' => 'Reason for canceling',
							'type' => 'raw',
							'value' => $invoice->delete_reason,
							'visible' => $invoice->deleted
						),
						array(
							'name' => 'invoice_date',
							'type' => 'raw',
							'value' => Yii::app()->dateFormatter->formatDateTime(strtotime($invoice->invoice_date), "short", null)
						),
						array(
							'name' => 'due_date',
							'type' => 'raw',
							'value' => isset($invoice->due_date) ? Yii::app()->dateFormatter->formatDateTime(strtotime($invoice->due_date), "short", null) : "-"
						),
						array(
							'label' => 'Invoice Total',
							'type' => 'raw',
							'value' => Yii::app()->numberFormatter->formatCurrency($invoice->getInvoiceTotal(), $invoice->currency_code)
						),
						array(
							'label' => 'Total Amount Paid',
							'type' => 'raw',
							'value' => Yii::app()->numberFormatter->formatCurrency($invoice->getAmountPaid(), $invoice->currency_code)
						),
						array(
							'label' => 'Balance',
							'type' => 'raw',
							'value' => Yii::app()->numberFormatter->formatCurrency($invoice->getBalance(), $invoice->currency_code)
						),
					),
					'htmlOptions' => array('class' => 'summary-table')
				));
				?>
			</div>
		</div>
	</div>
	<div id="invoiceItems" class="grid_16 section">
		<div class="block">
			<h2>Invoice Items</h2>
			<?php
			$this->widget('zii.widgets.grid.CGridView', array(
				'dataProvider' => $itemsDataProvider,
				'template' => '{items}',
				'emptyText' => $emptyText,
				'htmlOptions' => array(
					'class' => 'grid-view grey'
				),
				'columns' => array(
					'id' => array(
						'header' => 'No.',
						'value' => '$row + 1',
						'htmlOptions' => array(
							'class' => 'item-row valign-top',
							'width' => 30
						)
					),
					'description' => array(
						'name' => 'description',
						'value' => 'nl2br($data->description)',
						'type' => 'raw',
						'htmlOptions' => array(
							'class' => 'item-description valign-top'
						)
					),
					'transaction' => array(
						'header' => 'Property',
						'name' => 'transaction',
						'value' => '$data->propertyCase->property->prop_address_formatted',
						'htmlOptions' => array(
							'class' => 'item-description valign-top'
						),
						'visible' => false
					),
					'quantity' => array(
						'name' => 'quantity',
						'headerHtmlOptions' => array(
							'class' => 'item-quantity'
						),
						'htmlOptions' => array(
							'class' => 'item-quantity valign-top'
						),
						'visible' => false
					),
					'unit_price' => array(
						'name' => 'unit_price',
						'type' => 'raw',
						'value' => 'Yii::app()->numberFormatter->formatCurrency($data->unit_price, $data->invoice->currency_code)',
						'headerHtmlOptions' => array(
							'class' => 'item-unit-price'
						),
						'htmlOptions' => array(
							'class' => 'item-unit-price valign-top'
						)
					),
					'gst' => array(
						'header' => 'GST',
						'name' => 'gst_amount',
						'value' => 'Yii::app()->numberFormatter->formatCurrency($data->gst_amount, $data->invoice->currency_code)',
						'headerHtmlOptions' => array('class' => 'gst-amount'),
						'htmlOptions' => array('class' => 'gst-amount valign-top')
					),
					'amount' => array(
						'name' => 'amount',
						'type' => 'raw',
						'value' => 'Yii::app()->numberFormatter->formatCurrency($data->amount, $data->invoice->currency_code)',
						'headerHtmlOptions' => array('class' => 'item-amount'),
						'htmlOptions' => array(
							'class' => 'item-amount valign-top'
						)
					),
				),
			));
			?>
		</div>
	</div>
	<?php if ($invoice->invoice_type_code == 'Invoice'): ?>
		<div id="paymentItems" class="grid_16 section">
			<div class="block">
				<h2>Payments Received</h2>
				<?php
				$this->widget('zii.widgets.grid.CGridView', array(
					'dataProvider' => $paymentItemsDataProvider,
					'template' => '{items}',
					'emptyText' => $emptyText,
					'htmlOptions' => array(
						'class' => 'grid-view grey'
					),
					'rowCssClassExpression' => '($data->deleted == 1 ? "deleted" : "")',
					'columns' => array(
						array(
							'header' => 'No.',
							'value' => '$row + 1',
							'htmlOptions' => array(
								'width' => 30
							)
						),
						array(
							'header' => 'Receipt Number',
							'name' => 'payment_number',
							'type' => 'raw',
							'value' => 'CHtml::link("#" . $data->payment_number, array("receipt/view", "id" => $data->id), array("class" => "blue link"))',
						),
						array(
							'header' => 'Date Received',
							'name' => 'payment_date',
							'type' => 'raw',
							'value' => 'Yii::app()->dateFormatter->formatDateTime(strtotime($data->payment_date), "short", null)',
						),
						'payer_name',
						array(
							'name' => 'payment_method_type_code',
						),
						'comment',
						array(
							'name' => 'delete_reason',
							'visible' => $showDeleted
						),
						array(
							'header' => 'Amount Received',
							'name' => 'amount',
							'type' => 'raw',
							'value' => 'Yii::app()->numberFormatter->formatCurrency($data->sub_amount, $data->currency_code)',
							'htmlOptions' => array('class' => 'payment-amount-column',),
							'headerHtmlOptions' => array('class' => 'payment-amount-column')
						),
//					array(
//						'header' => '%',
////						'value' => 'Yii::app()->format->formatPercentage($data->)',
//						'htmlOptions' => array('class' => 'payment-amount-column',),
//						'headerHtmlOptions' => array('class' => 'payment-amount-column')
//					),
						array(
							'header' => 'Balance',
							'name' => 'sub_amount',
							'initialValue' => $invoice->getInvoiceTotal(),
							'value' => 'Yii::app()->numberFormatter->formatCurrency($total, $data->currency_code)',
							'class' => 'common.widgets.grid.PsTotalColumn',
							'reducing' => true,
							'htmlOptions' => array('class' => 'balance-amount-column',),
							'headerHtmlOptions' => array('class' => 'balance-amount-column')
						),
						array(
							'header' => 'Actions',
							'class' => 'common.widgets.grid.PsActionColumn', // protected/components/PsActionColumn.php
							'htmlOptions' => array(
								'class' => 'action-column',
								'width' => 80,
							),
							'options' => array(
								'width' => 120,
//                                'onSelect' => 'js:propsage.propertycase.update.onFormActionSelect'
							),
							'actions' => array(
								'delete' => array(
									'label' => 'Cancel Receipt',
									'icon' => '/images/icons/fugue/cross-circle.png',
									'class' => 'case-payment-action-delete',
									'url' => array('receipt/cancel', 'id' => '$data->id'),
								),
							)
						),
					),
				));
				?>
				<?php if ($invoice->hasDeletedPayments()): ?>
					<?php if (!$showDeleted): ?>
						<a class="float-right blue link" href="<?php echo $this->createUrl('invoice/view', array('id' => $invoice->id, 'showDeleted' => true)); ?>">Show Deleted</a>
					<?php else: ?>
						<a class="float-right blue link" href="<?php echo $this->createUrl('invoice/view', array('id' => $invoice->id)); ?>">Hide Deleted</a>
					<?php endif; ?>
				<?php endif; ?>
			</div>
		</div>
	<?php endif; ?>
</div>

<div id="emailInvoiceDialog" style="display: none;" title="Send Email">
	<?php
	$form = $this->beginWidget('PsActiveForm', array(
		'id' => 'send-email-form',
		'action' => array('invoice/ajaxEmailInvoice', 'id' => $invoice->id),
		'enableAjaxValidation' => false,
		'enableClientValidation' => true,
		'clientOptions' => array(
			'inputContainer' => 'tr',
			'validationDelay' => 0,
			'validateOnSubmit' => true,
	)));
	?>
	<table class="form-table">
		<tr>
			<th scope="row" width="100"><?php echo $form->labelEx($emailForm, 'parties', array('label' => 'To')); ?></th>
			<td>
				<div id="EmailForm_recipients_ct">
					<?php
					$this->widget('common.widgets.PsSelect2.PsSelect2', array(
						'model' => $emailForm,
						'attribute' => 'recipients',
						'data' => $billToEmails,
						'htmlOptions' => array(
							'style' => 'width: 610px',
							'multiple' => true,
							'data-placeholder' => 'Select emails'
						)
					));
					?>
					<?php echo $form->error($emailForm, 'parties', false); ?>
				</div>
				<a id="btnAddEmail" class="blue link" href="#">
					<img src="/images/icons/fugue/plus-button.png" class="valign-middle" />
					Add Email
				</a>
			</td>
		</tr>
		<tr>
			<th>Subject</th>
			<td>
				<?php
				echo $form->textField($emailForm, 'subject', array(
					'style' => 'width: 100%'
				));
				?>
			</td>
		</tr>
		<tr>
			<th>Message</th>
			<td>
				<?php
//				echo $form->textArea($emailContactsForm, 'message', array(
//					'style' => 'width: 100%',
//					'rows' => 8
//				));
				?>
				<?php
				$this->widget('common.lib.ckeditor.CKEditorWidget', array(
					'model' => $emailForm,
					'attribute' => 'message',
					'htmlOptions' => array(
						'style' => 'width: 100%',
						'rows' => 8
					),
					"config" => array(
						"toolbar" => "Basic",
					),
					#Optional address settings if you did not copy ckeditor on application root
					"ckEditor" => Yii::app()->basePath . "/../html/js/ckeditor/ckeditor.php",
					# Path to ckeditor.php
					"ckBasePath" => Yii::app()->request->hostInfo . "/js/ckeditor/",
						# Realtive Path to the Editor (from Web-Root)
				));
				?>
			</td>
		</tr>
		<tr>
			<th>Attachments</th>
			<td>
				<img src="/images/icons/farmfresh/16x16/file_extension_pdf.png" class="valign-middle" /> <strong>invoice.pdf</strong>
			</td>
		</tr>
	</table>
	<?php $this->endWidget(); ?>
</div>
<div id="addEmailDialog" style="display: none;" title="Add Email">
	<?php
	$form = $this->beginWidget('PsActiveForm', array(
		'id' => 'add-email-form',
		'action' => array('ajax/contact/addEmail', 'party_id' => $invoice->to_party_id),
		'enableAjaxValidation' => false,
		'enableClientValidation' => true,
		'clientOptions' => array(
			'inputContainer' => 'tr',
			'validationDelay' => 0,
			'validateOnSubmit' => true,
	)));
	?>
	<table class="form-table no-margin">
		<tr>
			<td>
				<input id="new_email" name="new_email" type="text" size="60" />
			</td>
		</tr>
	</table>

	<?php $this->endWidget(); ?>
</div>